package com.htx.zqs.blesmartmask.utils;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.util.Log;

import com.htx.zqs.blesmartmask.R;

/**
 * Created by Administrator on 2018/8/14.
 */

public class RingtoneUtil {
    private static RingtoneUtil instance = null;
    private MediaPlayer mMp;
    private Vibrator    mVibrator;
    private Context     context;

    public static RingtoneUtil getInstance() {
        if (instance == null) {
            instance = new RingtoneUtil();
        }
        return instance;
    }

    public RingtoneUtil() {
    }

    public RingtoneUtil(Context context) {
        this.context = context;
    }

    private void soundStart() {
        if (mMp == null) {
            mMp = new MediaPlayer();
        }
        try {
//            mMp.setDataSource(context, RingtoneManager
//                    .getDefaultUri(RingtoneManager.TYPE_RINGTONE));//这里我用的来电声音，还有其他的，大家可以点进去看

            mMp = MediaPlayer.create(context, R.raw.ringtone);//这里我用的来电声音，还有其他的，大家可以点进去看
            Log.e("==1", "=====" + 5555);
            mMp.setLooping(true); //循环播放
            //   mMp.prepare();
            Log.e("==2323232", "=====" + 5555);
            mMp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setSoundAndShock() {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int ringerMode = audioManager.getRingerMode();
        switch (ringerMode) {
            case AudioManager.RINGER_MODE_SILENT://静音
                //do sth
                Log.e("==1", "=====" + ringerMode);
                shockStart();
                break;
            case AudioManager.RINGER_MODE_NORMAL://响铃
                //do sth
                Log.e("==2", "=====" + ringerMode);
                shockStart();
                soundStart();
                break;
            case AudioManager.RINGER_MODE_VIBRATE://震动
                //do sth
                Log.e("==3", "=====" + ringerMode);
                shockStart();
                Log.e("==4", "=====" + ringerMode);
                break;
        }
    }

    public void stopSound() {
        if (mMp != null) {
            mMp.stop();
        }
        mMp = null;
    }

    public void stopShock() {
        if (mVibrator != null) {
            mVibrator.vibrate(new long[]{0, 1}, -1);
            mVibrator.cancel();
        }
        mVibrator = null;
    }

    private void shockStart() {
        //取得震动服务的句柄
        if (mVibrator == null) {
            mVibrator = (Vibrator) (context.getSystemService(Service.VIBRATOR_SERVICE));
        }
        Log.e("==1", "=====" + 6666666);
        //按照指定的模式去震动。
//				vibrator.vibrate(1000);
        //数组参数意义：第一个参数为等待指定时间后开始震动，震动时间为第二个参数。后边的参数依次为等待震动和震动的时间
        //第二个参数为重复次数，-1为不重复，0为一直震动
        mVibrator.vibrate(new long[]{1000, 1000}, 0);
    }

}
